﻿using System;
using System.Collections.Specialized;
using ImpEx.Engine;

namespace ImpEx.ConsoleApp
{
    internal class Program
    {
        #region Методы класса

        private static void DisplayRestoreHelp()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Restore usage:");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(
                "ImpEx.Console.exe -o restore -site siteUrl [-web subwebRelativePath] -packFile packFileName");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("");
            Console.WriteLine("Exapmple:");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(
                "ImpEx.Console.exe -o restore -site http://localhost/sites/mysite -web /Reports -rule mysite.rules -packFile packedMySite.cmp.zip");
            Console.WriteLine();
            Console.WriteLine(
                "ImpEx.Console.exe -o restore -site http://localhost -rule mysite.rules -packFile packedMySite.cmp.zip");
            Console.WriteLine();
        }

        private static void DisplayBackupHelp()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Backup usage:");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(
                "ImpEx.Console.exe -o backup -site siteUrl [-web subwebRelativePath] -ruleFile pathRulesFile -packFile packFileName");
            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Exapmple:");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(
                "ImpEx.Console.exe -o backup -site http://localhost/sites/mysite -ruleFile mysite.rules -packFile packedMySite.cmp.zip");
            Console.WriteLine();
            Console.WriteLine(
                "ImpEx.Console.exe -o backup -site http://localhost -web /Reports -ruleFile mysite.rules -packFile packedMySite.cmp.zip");
            Console.WriteLine();
        }

        private static NameValueCollection GetArguments(string[] args)
        {
            NameValueCollection valueCollection = new NameValueCollection();
            for (int i = 0; i < args.Length; i += 2)
            {
                if (i + 1 < args.Length)
                {
                    valueCollection.Add(args[i].ToLowerInvariant(), args[i + 1]);
                }
            }
            return valueCollection;
        }

        private static bool ValidateArguments(NameValueCollection valueCollection)
        {
            switch (valueCollection["-o"])
            {
                case "backup":
                    if (valueCollection["-site"] != null
                        && valueCollection["-site"].ToLowerInvariant().StartsWith("http://")
                        && valueCollection["-ruleFile"] != null
                        && valueCollection["-packFile"] != null
                        )
                    {
                        BackupOperaton(valueCollection);
                    }
                    else
                    {
                        DisplayBackupHelp();
                        return false;
                    }

                    break;

                case "restore":
                    if (valueCollection["-site"] != null
                        && valueCollection["-site"].ToLowerInvariant().StartsWith("http://")
                        && valueCollection["-packFile"] != null
                        )
                    {
                        RestoreOperaton(valueCollection);
                    }
                    else
                    {
                        DisplayRestoreHelp();
                        return false;
                    }
                    break;

                default:
                    DisplayRestoreHelp();
                    DisplayBackupHelp();
                    return false;
            }
            ;
            return true;
        }

        private static void BackupOperaton(NameValueCollection arguments)
        {
            Console.WriteLine("Backup Processing:");
            Console.WriteLine("-Site: " + arguments["-site"]);

            if (arguments["-web"] != null)
            {
                Console.WriteLine("-Web: " + arguments["-web"]);
            }

            Console.WriteLine("-Rule: " + arguments["-ruleFile"]);
            Console.WriteLine("-packFile: " + arguments["-packFile"]);
            Console.WriteLine();

            string webUrl = arguments["-web"] ?? "/";
            SPSWebApplication webApplication = new SPSWebApplication(arguments["-site"], webUrl);

            Logger.OnWriteLine += Console.WriteLine;

            SPSProgressBar progressBar = new SPSProgressBar();

            Console.WriteLine("Applying rules");
            ExportRulesManager.LoadRules(arguments["-ruleFile"], webApplication, progressBar);

            WSSExport import = new WSSExport();
            import.Export(arguments["-packFile"], webApplication);
        }

        private static void RestoreOperaton(NameValueCollection arguments)
        {
            Console.WriteLine("Restore Processing:");
            Console.WriteLine("-Site: " + arguments["-site"]);

            if (arguments["-web"] != null)
            {
                Console.WriteLine("-Web: " + arguments["-web"]);
            }

            Console.WriteLine("-packFile: " + arguments["-packFile"]);
            Console.WriteLine();

            string webUrl = arguments["-web"] ?? "/";

            SPSWebApplication webApplication = new SPSWebApplication(arguments["-site"], webUrl);

            WSSImport import = new WSSImport();
            import.Import(arguments["-packFile"], webApplication);
        }

        private static void Main(string[] args)
        {
            Console.WriteLine("WSS Content Import/Export Console [Version 1.0.0]");
            Console.WriteLine("(C) Copyright 2008-2009 Evgeny Zyuzin");
            Console.WriteLine("");

            NameValueCollection arguments = GetArguments(args);
            if (!ValidateArguments(arguments))
            {
                Console.WriteLine("Press any key.");
                Console.ReadKey();
                return;
            }
        }

        #endregion
    }
}